package org.spring.springboot.controller;

import java.util.UUID;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayTradeCreateRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayTradeCreateResponse;

@Controller
public class RouterController {

	@RequestMapping("Login")
	public String login(@RequestParam String code, @RequestParam String id) {
		return "redirect:/#/shop/id/" + id + "?code=" + code;
	}

	
	@ResponseBody
	@RequestMapping("alipay")
	public JSON alipay(String alipay_id,String fee ,HttpServletRequest req, HttpServletResponse res) throws AlipayApiException {
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2017111509946876",
				"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDrJIWv+RDAQYibsqpDkEcv+nLGYkQhUZqx06z0Lky1sxfopIwIUg2accyQK+v/tX/eOWCtk6dzeybchAN00dzhiUM4KGAqf6WQpxU3c0zTUhTdoZ0TF37xxrhJoWYBBiiA54fHHsydkBnfFGDm5HnHQZIRfPmyQ1+TO9DAo1JKIApXWL056bMRthuZJfxZc6YLuviY2ix5P6hqTps1NmyK8gUXE+6S2dde+BbB5BGt5vHQYwt1kLcrzWtk01kyJQ4xKNdrjW5JVHaFj5Ua7eVMOydhcPbODgYldhyy3uL4ihzLqLZ9k81YmG4ETPiNW+rHuV+tRmK8IVrtowg3/l8bAgMBAAECggEADaFtWnkOnoZt+9CMjCQV7FMRSiyyFFhdpkdF9FWmvrWmAidfsq/G+Bf2qrjREl6cDe63ivhPMaOG6iu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rVAUgZt/UecSgVFK/asv8q6PdVXbBptJ/7PV6b04tSgobTY9ZIeq9AvJkrnHK/0=",
				"json", "GBK",
				"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApBpvmV+fJhNdFMIV2vSmUbjqlAP7wB//sMNgZlbbIC+E6dEdGOooASCQVTSUCNVNet7hnWk4d1UD38fLrw2CEgTKhz4LuTBC50yEVUgyowBFSzOLC8afKTE1KYa0L+L0xDNWMN7VnjdK3qK6efIFQdlW6nuYoKiahlEZcwxdf8wJo5QhI+E0EQGBZNB77HbJDpmSY8g2uCDkrntKE7uW7nc0IHRaCVzXTd461eX2FyIkFoUHegmlvOuDBmosKgsUgm7qdhbRD6Pd1LntffXUIvkjBGVZ0CS8p6HBqYitznu5xqb0+rJLWmQC6VNqnRkfec+5aVwt3htBYLVNIS4rUwIDAQAB",
				"RSA2");
		AlipayTradeCreateRequest request = new AlipayTradeCreateRequest();
		request.setBizContent(
				"{" + "\"out_trade_no\":\""+UUID.randomUUID().toString().substring(0, 30).replace('-','_')+"\"," + "\"total_amount\":"+fee+","
						+ "\"subject\":\"奎家生活\"," + "\"buyer_id\":\""+alipay_id+"\"" + "}");

		AlipayTradeCreateResponse response = alipayClient.execute(request);

		JSONObject json = new JSONObject();

		if (response.isSuccess()) {

			json.put("tradeNo", response.getTradeNo());

		}
		return json;

	}

	@ResponseBody
	@RequestMapping("getAlipayId")
	public JSON getAlipayId(@RequestParam String auth_code) throws AlipayApiException {

		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2017111509946876",
				"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDrJIWv+RDAQYibsqpDkEcv+nLGYkQhUZqx06z0Lky1sxfopIwIUg2accyQK+v/tX/eOWCtk6dzeybchAN00dzhiUM4KGAqf6WQpxU3c0zTUhTdoZ0TF37xxrhJoWYBBiiA54fHHsydkBnfFGDm5HnHQZIRfPmyQ1+TO9DA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X5Cm/wOIhVAN0k6tqUKABfaZ4Psguihuu6hdzQFnrwg1hh1xz3JlHwPBzmOOcs97v2WCh8ymmJsrf7U+MF0tR1xTusMWhl1yBaVXZqa4HtiLy2VwI6y/qXN29JvYMuGh9IyTkdD6aD4BSuR8XwKhAoGAf2ik5us4zmAXxu3lkkxglgoWANdS1dyxFaEUyJ+aymY1zPO9+dQkhBV4ofKrmsK12DaUJr1g/gI7QYwqmU5HyGUagFlNMpOxx3sTIw/m1Gf8rVAUgZt/UecSgVFK/asv8q6PdVXbBptJ/7PV6b04tSgobTY9ZIeq9AvJkrnHK/0=",
				"json", "GBK",
				"MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApBpvmV+fJhNdFMIV2vSmUbjqlAP7wB//sMNgZlbbIC+E6dEdGOooASCQVTSUCNVNet7hnWk4d1UD38fLrw2CEgTKhz4LuTBC50yEVUgyowBFSzOLC8afKTE1KYa0L+L0xDNWMN7VnjdK3qK6efIFQdlW6nuYoKiahlEZcwxdf8wJo5QhI+E0EQGBZNB77HbJDpmSY8g2uCDkrntKE7uW7nc0IHRaCVzXTd461eX2FyIkFoUHegmlvOuDBmosKgsUgm7qdhbRD6Pd1LntffXUIvkjBGVZ0CS8p6HBqYitznu5xqb0+rJLWmQC6VNqnRkfec+5aVwt3htBYLVNIS4rUwIDAQAB",
				"RSA2");
		AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
		request.setCode(auth_code);
		request.setGrantType("authorization_code");
		
		JSONObject json = new JSONObject();
		
		try {
			AlipaySystemOauthTokenResponse oauthTokenResponse = alipayClient.execute(request);
			
			json.put("alipay_id", oauthTokenResponse.getUserId());
		} catch (AlipayApiException e) {
			// 处理异常
			e.printStackTrace();
		}
		return json;

	}

}
